﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


// machine ID + type + Y/N
namespace Hypermarket_Serial_Port
{
    class LogInManager : CashierRegisterTaskManager
    {
        SecurityManager mSManager;
        #region Constructor
        public LogInManager()
        {
            mSManager = SecurityManager.getInstance();
        }
        #endregion


        #region Operation Login
        //received a list of string from controller
        public override List<string> TaskOperation(List<string> receivedInfoList, Dictionary<string, CashierRegister> cashiersDict)
        {
            try
            {
                SerialPortLog.Log("Login Start");
                //string machineID = (int.Parse(s_receivedInform.Substring(1, 1))).ToString().PadLeft(4, '0');
                string machineID = receivedInfoList[Constant.ZERO];
                string type = receivedInfoList[Constant.ONE];
                string cashierID = receivedInfoList[Constant.TWO];
                string cashierPassword = receivedInfoList[Constant.THREE];
                cashierPassword = mSManager.Encrypt(cashierPassword);
                SerialPortLog.LogOperation("LogIn: " + machineID);
                //string endCheckByte = s_receivedInform.Substring(11, 1);
                if (CheckValidLogin(cashierID, cashierPassword))
                {
                    CashierRegister cs;
                    if (cashiersDict.TryGetValue(machineID, out cs))
                    {
                        cs.CancelAllTransactions();
                        List<string> output = new List<string>();
                        output.Add(machineID);
                        output.Add(type);
                        output.Add(Constant.SUCCESS);
                        SerialPortLog.LogOperation("LogIn Successfully: " + machineID);
                        SerialPortLog.Log("Login Successfully");
                        return output;
                    }
                    else
                    {
                        CashierRegister cashier = new CashierRegister(cashierID, machineID);
                        cashiersDict.Add(machineID, cashier);
                        List<string> output = new List<string>();
                        output.Add(machineID);
                        output.Add(type);
                        output.Add(Constant.SUCCESS);
                        SerialPortLog.LogOperation("LogIn Successfully: " + machineID);
                        SerialPortLog.Log("Login Successfully");
                        return output;
                    }
                }
                else
                {
                    List<string> tempStr = new List<string>();
                    tempStr.Add(machineID);
                    tempStr.Add(type);
                    tempStr.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("LogIn Fail: " + machineID);
                    SerialPortLog.Log("Login Fail");
                    return tempStr;
                }   
            }
            catch
            {
                //List<string> tempStr = new List<string>();
                //tempStr.Add(Constant.PROGRAM_ERROR);
                //return tempStr;
                SerialPortLog.Log("Login Error");
                throw;
            }
        }
        #endregion 


        #region Check Valid Login Assessment
        private bool CheckValidLogin(string cashierID, string cashierPassword)
        {
            DataTable cashierTable = mDBManager.getTable(Constant.CASHIER_TABLE);
            string query = Constant.CASHIER_ID + Constant.EQUAL + Constant.SINGAL_QUOTE + cashierID +
                            Constant.SINGAL_QUOTE + Constant.AND + Constant.CASHIER_PASSWORD + 
                            Constant.EQUAL + Constant.SINGAL_QUOTE + cashierPassword + Constant.SINGAL_QUOTE;
            DataRow[] row = cashierTable.Select(query);
            if (row.Count() == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion 

    }
}
